package com.shengde.jinwawaforleader.model.homePage;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.activity.LoginActivity;
import com.shengde.jinwawaforleader.base.BaseActivity;
import com.shengde.jinwawaforleader.bean.User;
import com.shengde.jinwawaforleader.network.BaseProtocol;
import com.shengde.jinwawaforleader.network.protocolbean.ProShowClickGood;
import com.shengde.jinwawaforleader.utils.MD5;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * 重置密码页面
 * Created by Administrator on 2016/11/24 0024.
 */
public class SettingPasswordActivity extends BaseActivity {
	private static final String TAG = "SettingPasswordActivity";
	private static final int FINSH = 1;
	private Button btn_next;   //下一步
	private EditText edt_num;  //填写密码
	private EditText edt_password_num;  //确认密码
	private TextView tv_telephone;  //密码
	private TextView tv_title;
	private TextView tv_back;
	private String telephoneNum;

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
				case FINSH:
					Intent intent = new Intent(SettingPasswordActivity.this, LoginActivity.class);
					startActivity(intent);
					Toast.makeText(SettingPasswordActivity.this, getString(R.string.detail_modify_success), Toast.LENGTH_SHORT).show();
					break;
			}
		}
	};

	@Override
	public void onRoot(Bundle savedInstanceState) {
		setContentView(R.layout.find_password_activity);
	}

	@Override
	public void initView() {
		tv_back = (TextView) findViewById(R.id.tv_back);
		tv_title = (TextView) findViewById(R.id.tv_title);
		tv_telephone = (TextView) findViewById(R.id.tv_telephone);
		edt_num = (EditText) findViewById(R.id.edt_num);
		edt_password_num = (EditText) findViewById(R.id.edt_password_num);
		btn_next = (Button) findViewById(R.id.btn_next);
	}

	@Override
	public void initData() {
		tv_title.setText(getString(R.string.setting_password));
		tv_telephone.setText(getString(R.string.tv_password));
		edt_num.setHint(getString(R.string.into_password));
		btn_next.setText(getString(R.string.sure_success));
		telephoneNum = getIntent().getStringExtra("telephoneNum");

	}

	@Override
	public void initListener() {
		btn_next.setOnClickListener(this);
		tv_back.setOnClickListener(this);
	}

	@Override
	public void onWidgetClick(View view) {
		switch (view.getId()) {
			case R.id.tv_back:
				finish();
				break;
			case R.id.btn_next:
				if (TextUtils.isEmpty(edt_num.getText().toString().trim())) {
					Toast.makeText(this, getString(R.string.password_not_empty), Toast.LENGTH_SHORT).show();
				} else if (edt_num.getText().toString().trim().length() > 18 || edt_num.getText().toString().trim().length() < 6) {
					Toast.makeText(this, "密码不能少于6位或者多于18位", Toast.LENGTH_SHORT).show();
				} else if (edt_password_num.getText().toString().trim().equals(edt_num.getText().toString().trim())) {
					settingPassword();
				} else {
					Toast.makeText(this, getString(R.string.detail_modify_different), Toast.LENGTH_SHORT).show();
				}
				break;
		}
	}

	/**
	 * 设置新密码
	 */
	private void settingPassword() {
		String url = "";
		if (User.isTest) {
			url = BaseProtocol.HOST_BASE_TEST + "/sms/PwdValidate";
		} else {
			url = BaseProtocol.HOST_BASE + "/sms/PwdValidate";
		}
		RequestParams params = BaseProtocol.getRequestParams(url);
		params.addParameter("phone", telephoneNum);
		params.addParameter("newPwd", MD5.getMD5(edt_password_num.getText().toString().trim()));
		x.http().post(params, new Callback.CommonCallback<String>() {

			@Override
			public void onSuccess(String result) {
				ProShowClickGood proSHowGetNote = new Gson().fromJson(result, ProShowClickGood.class);
					Log.i(TAG, "onSuccess: code = " + proSHowGetNote.getCode());
				if (proSHowGetNote.getCode() == 0) {
					mHandler.sendEmptyMessage(FINSH);
				}
			}

			@Override
			public void onError(Throwable ex, boolean isOnCallback) {

			}

			@Override
			public void onCancelled(CancelledException cex) {

			}

			@Override
			public void onFinished() {

			}
		});
	}
}
